package com.minimalist.basic.controller.tc;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import com.minimalist.common.config.mybatis.bo.PageResp;
import com.minimalist.common.entity.tc.entity.vo.plateNumber.TcPlateNumberQueryVO;
import com.minimalist.common.entity.tc.entity.vo.plateNumber.TcPlateNumberVO;
import com.minimalist.common.feign.client.tc.TcPlateNumberFeignClient;
import com.minimalist.common.utils.Add;
import com.minimalist.common.utils.ImageService;
import com.minimalist.common.utils.Update;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author 11987
 */
@Validated
@RestController
@Tag(name = "停车_车牌信息管理")
@RequestMapping("/tc/plateNumber")
public class TcPlateNumberController{
    private static final Logger log = LoggerFactory.getLogger(TcPlateNumberController.class);


    @Autowired
    private TcPlateNumberFeignClient feignClient;

    @Autowired
    private ImageService imageService;

    @PostMapping("/add")
    @SaCheckPermission("tc:plateNumber:add")
    @Operation(summary = "添加车牌信息")
    public ResponseEntity<Void> add(
            @RequestBody @Validated(Add.class) TcPlateNumberVO plateNumberVO){
        return feignClient.addPlateNumber(plateNumberVO);
    }

    @DeleteMapping("/delete")
    @SaCheckPermission("tc:plateNumber:delete")
    @Operation(summary = "删除车牌信息 -> 根据车牌信息ID删除")
    public ResponseEntity<Void> delete(
            @RequestParam("plateNumberId") @NotNull(message = "车牌信息ID不能为空") @Parameter(name = "plateNumberId", required = true, description = "车牌信息ID") Long plateNumberId){
        return feignClient.deletePlateNumber(plateNumberId);
    }

    @PutMapping("/update")
    @SaCheckPermission("tc:plateNumber:update")
    @Operation(summary = "修改车牌信息")
    public ResponseEntity<Void> update(
            @RequestBody @Validated(Update.class) TcPlateNumberVO plateNumberVO){
        return feignClient.updatePlateNumber(plateNumberVO);
    }

    @GetMapping("/getPageList")
    @SaCheckPermission("tc:plateNumber:get")
    @Operation(summary = "查询车牌信息列表(分页)")
    public ResponseEntity<PageResp<TcPlateNumberVO>> getPageList(TcPlateNumberQueryVO queryVO){
        return feignClient.getPagePlateNumberList(queryVO);
    }

    @GetMapping("/get/{plateNumberId}")
    @SaCheckPermission("tc:plateNumber:get")
    @Operation(summary = "根据车牌信息ID查询车牌信息")
    public ResponseEntity<TcPlateNumberVO> get(
            @PathVariable(value = "plateNumberId") @NotNull(message = "车牌信息ID不能为空") @Parameter(name = "plateNumberId", description = "车牌ID", required = true) Long plateNumberId){
        return feignClient.getPlateNumber(plateNumberId);
    }

//    @GetMapping("insertPlateNumber/{count}")
//    @SaCheckPermission("tc:plateNumber:add")
//    @Operation(summary = "插入车牌信息")
//    public ResponseEntity<Void> recognize(
//            @PathVariable(value = "count") @NotNull(message = "数量不能为空") @Parameter(name = "count", description = "数量", required = true) Integer count){
//        feignClient.insertPlateNumber(count);
//        return ResponseEntity.ok().build();
//    }

//    @GetMapping("/startTask")
//    @SaIgnore
//    // @SaCheckPermission("tc:plateNumber:add")
//    @Operation(summary = "启动车牌信息持续处理任务")
//    public ResponseEntity<Map<String, Object>> startTask(
//            @RequestParam(value = "count", defaultValue = "1") @Parameter(name = "count", description = "每次处理数量", required = false) Integer count,
//            @RequestParam(value = "interval", defaultValue = "5") @Parameter(name = "interval", description = "间隔秒数") Integer interval){
//        boolean result = plateNumberTaskService.startContinuousPlateNumberTask(count, interval);
//        Map<String, Object> response = new HashMap<>(2);
//        response.put("success", result);
//        response.put("message", result ? "持续处理任务已启动" : "任务启动失败，可能任务已在运行中");
//        return ResponseEntity.ok(response);
//    }
//
//    @PostMapping("/stopTask")
//    @SaIgnore
//    // @SaCheckPermission("tc:plateNumber:add")
//    @Operation(summary = "停止车牌信息定时任务")
//    public ResponseEntity<Map<String, Object>> stopTask(){
//        boolean result = plateNumberTaskService.stopPlateNumberTask();
//        Map<String, Object> response = new HashMap<>(2);
//        response.put("success", result);
//        response.put("message", result ? "定时任务已停止" : "定时任务停止失败，可能任务未在运行中");
//        return ResponseEntity.ok(response);
//    }
//
//    @GetMapping("/taskStatus")
//    @SaIgnore
//    // @SaCheckPermission("tc:plateNumber:get")
//    @Operation(summary = "获取车牌信息定时任务状态")
//    public ResponseEntity<ScheduleTaskService.TaskStatus> getTaskStatus(){
//        return ResponseEntity.ok(plateNumberTaskService.getPlateNumberTaskStatus());
//    }

    private static final String savePath = "D:\\projects\\zhixin\\test";

    /**
     * 下载图片并保存到服务器
     *
     * @param url      图片URL
     * @param filename 保存的文件名
     * @return 保存结果
     */
    @GetMapping("/download")
    @SaIgnore
    public ResponseEntity<String> downloadImage(
            @RequestParam(value = "url") String url, @RequestParam(value = "filename") String filename){
        String savePath = "D:\\projects\\zhixin\\test\\" + filename; // 根据实际情况修改保存路径
        imageService.downloadImage(url, savePath);
        return ResponseEntity.ok("图片已保存到: " + savePath);
    }
}
